# -*- coding: utf-8 -*-
import asyncio
from alibabacloud_dysmsapi20170525.client import Client as Dysmsapi20170525Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_dysmsapi20170525 import models as dysmsapi_20170525_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


def create_client() -> Dysmsapi20170525Client:
    """
    使用AK&SK初始化账号Client
    @param access_key_id:
    @param access_key_secret:
    @return: Client
    @throws Exception
    """
    config = open_api_models.Config(
        # 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。,
        access_key_id="LTAI4GHoGkVBr1CT5C57Y64j",
        # 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。,
        access_key_secret="5apl1h6CDXS2IBdmpKIhaHXhgIAxJv",
    )
    config.endpoint = f"dysmsapi.aliyuncs.com"
    return Dysmsapi20170525Client(config)


async def main_async(key, old_value, new_value, change):
    client = create_client()
    send_sms_request = dysmsapi_20170525_models.SendSmsRequest(
        phone_numbers="13421830635,13714715087",
        sign_name="爱问健康",
        template_code="SMS_465312108",
        template_param='{"product_name":"%s","old_num":"%s","new_num":"%s","num":"%s"}'
        % (key, old_value, new_value, change),
    )
    runtime = util_models.RuntimeOptions()
    try:
        # 复制代码运行请自行打印 API 的返回值
        await client.send_sms_with_options_async(send_sms_request, runtime)
    except Exception as error:
        # 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
        # 错误 message
        print(error.message)
        # 诊断地址
        print(error.data.get("Recommend"))
        UtilClient.assert_as_string(error.message)


if __name__ == "__main__":
    asyncio.run(main_async("产品名称", 100, 50, 50))
